<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      
    >
      <el-form-item :label="$t('performance_name')" prop="performanceName">
        <el-input
          v-model="queryParams.performanceName"
          :placeholder="$t('please_enter', { name: $t('performance_name') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('performance_code')" prop="performanceCode">
        <el-input
          v-model="queryParams.performanceCode"
          :placeholder="$t('please_enter', { name: $t('performance_code') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['performance:performanceList:add']"
        >
          {{ $t("add_new_performance_appraisal") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['performance:performanceList:import']"
          >导入</el-button
        > -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['performance:performanceList:export']"
          >{{ $t("export") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <performanceTable v-loading="loading" :tableData="tableList">
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['performance:performanceList:view']"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['performance:performanceList:edit']"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['performance:performanceList:delete']"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </performanceTable>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改绩效格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="160px"
        :disabled="lookView"
      >
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("basic_information") }}</span>
          </div>
          <el-form-item :label="$t('performance_name')" prop="performanceName">
            <el-input
              v-model="form.performanceName"
              :placeholder="
                $t('please_enter', { name: $t('performance_name') })
              "
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item :label="$t('performance_code')" prop="performanceCode">
            <el-input
              v-model="form.performanceCode"
              :placeholder="
                $t('please_enter', { name: $t('performance_code') })
              "
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item :label="$t('total_score')" prop="performanceTotalScore">
            <el-input
              v-model.number="form.performanceTotalScore"
              :placeholder="$t('please_enter', { name: $t('total_score') })"
              :show-word-limit="true"
            />
          </el-form-item>
          <el-form-item :label="$t('remarks')" prop="remark">
            <el-input
              v-model="form.remark"
              :placeholder="$t('please_enter', { name: $t('remarks') })"
              :rows="4"
              :show-word-limit="true"
              type="textarea"
            />
          </el-form-item>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("questions_of_appraisal") }}</span>
          </div>
          <performanceTopic
            :tableData="form.topicList"
            :lookView="lookView"
            ref="performanceTopic"
          ></performanceTopic>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 20px">
          <div slot="header" class="clearfix">
            <span>{{ $t("appraisal_configuration") }}</span>
          </div>
          <el-form-item prop="isNeedSelfAssessment">
            <el-checkbox
              v-model="form.isNeedSelfAssessment"
              :true-label="1"
              :false-label="0"
              >{{ $t("employee_evaluation_required") }}</el-checkbox
            >
          </el-form-item>
          <el-form-item prop="isNeedDirectSupervisorReview">
            <el-checkbox
              v-model="form.isNeedDirectSupervisorReview"
              :true-label="1"
              :false-label="0"
              >{{ $t("direct_supervisor_evaluation_required") }}</el-checkbox
            >
          </el-form-item>
          <el-form-item prop="isAllowedViewed">
            <el-checkbox
              v-model="form.isAllowedViewed"
              :true-label="1"
              :false-label="0"
              >{{ $t("allow_performance_appraisal") }}</el-checkbox
            >
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!lookView">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>

    <!-- 导入弹窗 -->
    <importModal
      ref="importModal"
      title="绩效导入"
      downloadUrl="table/table/downloadTemplate.do"
      uploadUrl="table/table/importTemplate.do"
      downloadName="绩效导入模版"
      @refreshList="getList"
    ></importModal>
  </div>
</template>

<script>
import {
  listTable,
  getTable,
  delTable,
  addTable,
  updateTable,
} from "@/api/custom/performanceList";
import importModal from "@/views/components/custom/importModal.vue";
import performanceTopic from "./performanceTopic.vue";
import performanceTable from "@/views/custom/performanceGroup/performanceTable.vue";
import { isNumber } from "@/utils/elementRule";
export default {
  name: "Table",
  components: {
    importModal,
    performanceTopic,
    performanceTable,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 绩效格表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        performanceName: null,
        performanceCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        performanceName: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("performance_name"),
            }),
            trigger: "blur",
          },
        ],
        performanceCode: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("performance_code"),
            }),
            trigger: "blur",
          },
        ],
        performanceTotalScore: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("total_score"),
            }),
            trigger: "blur",
          },
          { validator: isNumber, trigger: "blur" },
        ],
      },
      lookView: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询绩效格列表 */
    getList() {
      this.loading = true;
      listTable(this.queryParams).then((response) => {
        this.tableList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        performanceName: null,
        performanceCode: null,
        remark: null,
        performanceTotalScore: null,
        isNeedSelfAssessment: null,
        isNeedDirectSupervisorReview: null,
        isAllowedViewed: null,
      };
      this.lookView = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("add");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTable(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t("modify");
      });
    },
    handleView(row) {
      this.reset();
      const id = row.id;
      getTable(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.lookView = true;
        this.title = this.$t("view");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.topicList = this.$refs.performanceTopic.topicList;
          this.form.questionsQuantity = this.form.topicList.length;
          if (this.form.id != null) {
            updateTable(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            addTable(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delTable(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/jxPerformance/export",
        {
          ...this.queryParams,
        },
        `${this.$t("performance_appraisal_list")}_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.importModal.showModal();
    },
  },
};
</script>
